package com.woniuxy.background.service.impl;

import com.github.pagehelper.PageInfo;
import com.woniuxy.background.dao.GoodsMapper;
import com.woniuxy.background.dao.OrderitemMapper;
import com.woniuxy.background.dao.OrdersMapper;
import com.woniuxy.background.dao.UserMapper;
import com.woniuxy.background.pojo.Orderitem;
import com.woniuxy.background.service.OrderitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderitemServiceImpl implements OrderitemService {

    @Autowired
    private OrderitemMapper orderitemMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return orderitemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Orderitem record) {
        return orderitemMapper.insert(record);
    }

    @Override
    public int insertSelective(Orderitem record) {
        return orderitemMapper.insertSelective(record);
    }

    @Override
    public Orderitem selectByPrimaryKey(Integer id) {
        return orderitemMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Orderitem record) {
        return orderitemMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Orderitem record) {
        return orderitemMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo selectAllByPage(Integer currentPage, Integer pageSize) {
        List<Orderitem> list = orderitemMapper.selectAllByPage(currentPage,pageSize);
        PageInfo pageInfo = new PageInfo(list);
        List<Orderitem> orderitemList = new ArrayList<>();
        for (Orderitem o : list){
            o.setGoods(goodsMapper.selectByPrimaryKey(o.getGoodsId()));
            o.setOrders(ordersMapper.selectByPrimaryKey(o.getOid()));
            o.setUser(userMapper.selectByPrimaryKey(o.getUserId()));
            orderitemList.add(o);
        }
        pageInfo.setList(orderitemList);
        return pageInfo;
    }

    @Override
    public Orderitem selectByOid(int id) {
        return orderitemMapper.selectByOid(id);
    }
}
